import type { LocaleType } from '@/locales'
import { i18n, setHtmlPageLang } from '@/locales'
import { useLocaleStore } from '@/store/modules/locale'

export function useLocale() {
    const localeStore = useLocaleStore()
    const getLocale = computed(() => localeStore.getLocale)

    async function changeLocale(locale: LocaleType) {
        const globalI18n = i18n.global
        const currentLocale = unref(globalI18n.locale)
        if (currentLocale === locale) {
            return locale
        }
        if (i18n.mode === 'legacy') {
            i18n.global.locale = locale
        } else {
            ;(i18n.global.locale as any).value = locale
        }
        setHtmlPageLang(locale)
        return locale
    }

    return {
        getLocale,
        changeLocale
    }
}
